package com.example.aigc_education.controller;

import com.example.aigc_education.anno.RequestKeyParam;
import com.example.aigc_education.anno.RequestLock;
import com.example.aigc_education.common.BusinessException;
import com.example.aigc_education.domain.request.AddTodoRequest;
import com.example.aigc_education.domain.request.UdTodoCompletedRequest;
import com.example.aigc_education.domain.request.UpdateTodoRequest;
import com.example.aigc_education.domain.vo.*;
import com.example.aigc_education.service.TodoV2Service;
import com.example.aigc_education.utils.ResultUtil;
import com.example.aigc_education.utils.SecurityUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Slf4j
@RequestMapping("/todo/v2")
@RestController
public class TodoV2Controller {
    @Autowired
    private TodoV2Service todoV2Service;

    /**
     * 添加待办
     *
     * @param addTodoRequest 添加待办DTO
     * @return 返回添加计划结果
     */
    @RequestLock(prefix = "addTodoV2:", expire = 3)
    @PostMapping("/add")
    public ResultUtil addTodo(@RequestKeyParam @RequestBody AddTodoRequest addTodoRequest) {
        Long userId = SecurityUtils.getUserId();
        if (userId == null) {
            throw new BusinessException("用户未登录");
        }
        IdVO result = todoV2Service.addTodo(userId, addTodoRequest);
        return ResultUtil.success(result);
    }



    /**
     * 修改待办
     *
     * @param updateTodoRequest 修改待办rq
     * @return 修改待RESULT
     */
    @RequestLock(prefix = "updateTodoV2:", expire = 3)
    @PostMapping("/update")
    public ResultUtil updateTodo(@RequestKeyParam @RequestBody UpdateTodoRequest updateTodoRequest) {
        UpdateTodoVO result = todoV2Service.updateTodo(updateTodoRequest);
        return ResultUtil.success("success",result);
    }

    /**
     * 修改待办完成状态
     * @param udTodoCompletedRq 修改待办rq
     * @return 修改待办结果
     * @throws BusinessException 业务异常
     */
    @RequestLock(prefix = "updateTodoCompleted:", expire = 3)
    @PostMapping("/update/completed")
    public ResultUtil updateTodoCompleted(@RequestKeyParam @RequestBody UdTodoCompletedRequest udTodoCompletedRq) {
        UdTodoCompletedVO result = todoV2Service.updateTodoCompleted(udTodoCompletedRq);
        return ResultUtil.success("success",result);
    }


    @RequestLock(prefix = "deleteTodosV2:", expire = 3)
    @DeleteMapping
    public ResultUtil deleteTodos(@RequestKeyParam @RequestBody Long[] ids) {
        try {
            Boolean result = todoV2Service.deleteTodos(ids);
            if (!result) {
                throw new BusinessException("删除失败");
            }
            return ResultUtil.success("删除成功");
        } catch (Exception e) {
            throw new BusinessException("删除异常");
        }
    }

//    首页的待办事项列表展示的是今天ddl和优先级排序的待办

    /**
     * 获取今天ddl和优先级排序的待办
     *
     * @return 返回今天ddl和优先级排序的待办
     */
    @GetMapping("/today")
    public ResultUtil getTodayTodos() {
        Long userId = SecurityUtils.getUserId();
        if (userId == null) {
            throw new BusinessException("用户未登录");
        }
        Map<String, List<TodoVO>> response = todoV2Service.getTodayTodos(userId);
        return ResultUtil.success(response);
    }

    @GetMapping("/all")
    public ResultUtil getAllTodos() {
        Long userId = SecurityUtils.getUserId();
        if (userId == null) {
            throw new BusinessException("用户未登录");
        }
        GetAllTodosVO response = todoV2Service.getAllTodos(userId);
        return ResultUtil.success(response);
    }
}
